﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using STLGROUP.Layers.Presentations.FormBase;
using STLGROUP.Layers.BLL;
using STLGROUP.Layers.Common;
using STLGROUP.Layers.DataAccessLayers;

namespace QuanLyBanHang
{
    /// <summary>
    /// MF009 - Hoa đơn sử dụng
    /// </summary>
    /// <author>viet sang</author>
    public partial class MF009 : frmManager
    {
        DT_HoaDonBLL _business;
        ToolStripButton _btnThanhToan;

        public MF009()
        {
            InitializeComponent();
            pnlChiTiet.Enabled = true;
            _business = new DT_HoaDonBLL();

            tsbBaoCao.Text = "In hóa đơn";
            tsbBaoCao.Click += new EventHandler(tsbBaoCao_Click);
            //_bindingSource.CurrentChanged += new EventHandler(_bindingSource_CurrentChanged);

            //add controls
            this._btnThanhToan = new ToolStripButton();
            this._btnThanhToan.AutoSize = true;
            this._btnThanhToan.ForeColor = System.Drawing.Color.Black;
            this._btnThanhToan.ImageTransparentColor = System.Drawing.Color.Magenta;
            this._btnThanhToan.Name = "btnThanhToan";
            this._btnThanhToan.Padding = new System.Windows.Forms.Padding(3, 0, 4, 0);
            this._btnThanhToan.Text = "Thanh toán công nợ";
            this._btnThanhToan.Visible = true;
            this._btnThanhToan.BackColor = Color.AliceBlue;
            _btnThanhToan.Click += new EventHandler(_btnThanhToan_Click);

            toolStrip1.Items.Insert(12, _btnThanhToan);
        }

        private void _btnThanhToan_Click(object sender, EventArgs e)
        {
            HoaDonSuDung hoaDon = this._bindingSource.Current as HoaDonSuDung;
            if (hoaDon != null && hoaDon.ConLai.HasValue && hoaDon.ConLai.Value>0)
            {
                PF004 frm = new PF004() { IdDonHang =hoaDon.IDHoaDon , Mode = 2 };
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Filter();
                    //Cache.Context.SaveChanges();
                }
            }
        }

        private void tsbBaoCao_Click(object sender, EventArgs e)
        {
            if (_bindingSource.Current != null)
            {
                HoaDonSuDung hoaDon = _bindingSource.Current as HoaDonSuDung;
                if(hoaDon!=null&&hoaDon.IDLoaiHoaDon.HasValue)
                {
                    if (hoaDon.IDLoaiHoaDon == (int)StructsEnums.EnumLoaiHoaDon.HoaDonBanLe)
                    {
                        RF007 frm = new RF007() { IDHoaDon = hoaDon.IDHoaDon };
                        frm.ShowDialog();
                    }
                    if (hoaDon.IDLoaiHoaDon == (int)StructsEnums.EnumLoaiHoaDon.HoaDonBanSi)
                    {
                        RF008 frm = new RF008() { IDHoaDon = hoaDon.IDHoaDon };
                        frm.ShowDialog();
                    }              
                }
            }
            else
            {
                UICommon.ShowMessgeString("Chưa chọn hóa đơn", "Thông báo");
            }
        }

        protected override void LoadData()
        {
            txtTuNgay.Value = DateTime.Today.Subtract(new TimeSpan(7, 0, 0, 0));
            txtDenNgay.Value = DateTime.Today.AddDays(1);
            base.LoadData();
        }

        protected override void Filter()
        {
            var data = (from hd in Cache.Context.DT_HoaDon
                       join cthd in Cache.Context.DT_ChiTietHoaDon on hd.IDHoaDon equals cthd.IDHoaDon
                       join kh in Cache.Context.DT_KhachHang on hd.IDDoiTac equals kh.IDKhachHang into leftJoin
                       from a in leftJoin.DefaultIfEmpty()
                       join nv in Cache.Context.DT_NhanVien on hd.IDNhanVien equals nv.IDNhanVien
                       where (hd.NgayTao.HasValue || txtTuNgay.Value<=hd.NgayTao)
                       &&(hd.NgayTao.HasValue || txtDenNgay.Value>=hd.NgayTao)
                       select new { 
                           hd.IDHoaDon,
                           hd.IDLoaiHoaDon,
                           hd.MaHoaDon,
                           hd.TongTien,
                           hd.DaTra,
                           ConLai = hd.TongTien - hd.DaTra,
                           nv.MaNhanVien,
                           HoTenNhanVien = nv.HoVaTenDem+" "+nv.TenNhanVien,
                           a.MaKhachHang,
                           HoTenKhachHang = a.HoDem+" "+a.Ten,
                           hd.NgayTao
                       }).Distinct();
            List<HoaDonSuDung> dataSource = data.ToList().ConvertAll<HoaDonSuDung>(s => new HoaDonSuDung { 
                IDHoaDon = s.IDHoaDon,
                IDLoaiHoaDon=s.IDLoaiHoaDon,
                MaHoaDon=s.MaHoaDon,
                TongTien=s.TongTien,
                DaTra = s.DaTra,
                ConLai= s.ConLai,
                MaNhanVien =s.MaNhanVien,
                HoTenNhanVien=s.HoTenNhanVien,
                MaKhachHang=s.MaKhachHang,
                HoTenKhachHang= s.HoTenKhachHang,
                NgayTao = s.NgayTao.HasValue? s.NgayTao.Value.ToString("dd/MM/yyyy"):string.Empty

            }).ToList();

            _bindingSource.DataSource = dataSource;
            //_business.GetHoaDonDaSuDung(txtTuNgay.Value, txtDenNgay.Value);
            this.stlDataGridView1.DataSource = _bindingSource;
        }

        public class HoaDonSuDung
        {
            public int IDHoaDon { get; set; }
            public int? IDLoaiHoaDon { get; set; }
            public string MaHoaDon { get; set; }
            public decimal? TongTien { get; set; }
            public decimal? DaTra { get; set; }
            public decimal? ConLai { get; set; }
            public string MaNhanVien { get; set; }
            public string HoTenNhanVien { get; set; }
            public string MaKhachHang { get; set; }
            public string HoTenKhachHang { get; set; }
            public string NgayTao { get; set; }

        }
    }
}
